
//
//  CLZRefreshHeader.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/19.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CLZRefreshHeader.h"
@interface CLZRefreshHeader()
@property (nonatomic, strong) UIImageView *imageView;
@property(weak, nonatomic) UIView   *headerFreshView;
@end;

@implementation CLZRefreshHeader

#pragma mark - 构造方法

- (void)prepare
{
    [super prepare];
    UIImage *image = [UIImage imageNamed:@"icon_shuaxin"];
    self.mj_h = image.size.height + 20.f;
    UIView *view = [[UIView alloc] init];
    [self addSubview:view];
    self.headerFreshView = view;
  
    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.image = image;
    [self.headerFreshView addSubview:imageView];
    self.imageView = imageView;
}

- (void)placeSubviews
{
    [super placeSubviews];
    UIImage *image = [UIImage imageNamed:@"icon_shuaxin"];
    self.headerFreshView.bounds = CGRectMake(0, 10, SCREEN_WIDTH, image.size.height);
    self.headerFreshView.center = CGPointMake(self.mj_w *0.5, self.mj_h*0.5);
    self.imageView.size = CGSizeMake(image.size.width, image.size.height);
    self.imageView.center = self.headerFreshView.center;
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
}
#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
}
#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
}
#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    switch (state) {
        case MJRefreshStateIdle:
            [self endAnimation];
            break;
        case MJRefreshStatePulling:
            [self endAnimation];
            break;
        case MJRefreshStateRefreshing:
            [self startAnimation];
            break;
        default:
            break;
    }
}

-(void)startAnimation
{
    CABasicAnimation *basicAni= [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAni.duration = 1;
    basicAni.repeatCount = MAXFLOAT;
    //    basicAni.repeatDuration = 3;
    basicAni.toValue = @(M_PI);
    [self.imageView.layer addAnimation:basicAni forKey:nil];
}

- (void)endAnimation
{
    [self.imageView.layer removeAllAnimations];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
